package controller;

import com.alibaba.fastjson2.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import pojo.User;
import service.UserService;
import util.Laydata;

import java.util.List;

@Controller
//为了让springmvc能识别这个类，需要加注解
@RequestMapping("/asyncuser")
//相当于servlet的路径

public class AsyncUserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/tolist")
    public String toList() {
        return "asyncuser/list";
    }
    @RequestMapping("/toregist")
    public String toRegist() {
        return "regist";
    }
    @RequestMapping("/registSave")
    @ResponseBody
    public String registSave(User user){
        int result=userService.addUser(user);
        if(result>0){
            return "redirect:/login";
        }else{
            return "false";
        }
    }
   @RequestMapping("/list")
   @ResponseBody//区分同步请求和异步请求
    public String list(User user, @RequestParam(value = "page",defaultValue = "1") Integer currentPage,
                       @RequestParam(value = "limit" ,defaultValue = "10") Integer pageSize, Model model){//model相当于request,@RequestParam(value = "page")会自己传进来,defaultValue = "1"默认传进来1
        //查询数据
       int totalCount=userService.count(user);
       //偏移量
       int offset=(currentPage-1)*pageSize;
       List<User> users= userService.findAll(user,offset,pageSize);//拿数据
       Laydata laydata=new Laydata(0,"查询成功",totalCount,users);
       return JSON.toJSONString(laydata);//通过工具，把对象转换成json
    }
    @RequestMapping("/toadd")
    public String toAdd() {
        return "asyncuser/add";
    }
    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(User user) {
        int result=userService.addUser(user);
        //不能返回视图
        if (result>0){
            return JSON.toJSONString(new Laydata(0,"添加成功"));//自定义200 还是0
        }else {
            return JSON.toJSONString(new Laydata(500,"添加失败"));
        }

    }
    @RequestMapping ("/delete/{id}")
    @ResponseBody
    public String deleteById(@PathVariable("id") int id) {
        int result = userService.deleteUserById(id);
        if (result > 0) {
            return JSON.toJSONString(new Laydata(0, "删除成功"));
        } else {
            return JSON.toJSONString(new Laydata(500, "删除失败"));
        }
    }

    @RequestMapping ("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") int id, Model model) {
        User user=userService.findUserById(id);
        model.addAttribute("user",user);
        return "asyncuser/update";
    }

    @RequestMapping ("/updateSave")
    @ResponseBody
    public String updateSave( User user) {
        int result = userService.updateUser(user);
        if (result > 0) {
            return JSON.toJSONString(new Laydata(0, "修改成功"));
        } else {
            return JSON.toJSONString(new Laydata(500, "修改失败"));
        }
    }

}

